//
//  PLGeneralInfo.m
//
//  Created by KimmanYip on 2017/9/26.
//
//

#import "PLGeneralInfo.h"
#import "NSDictionary+PLDefaultValue.h"
#import <SAMKeychain/SAMKeychain.h>
#import <UIKit/UIKit.h>

@implementation PLGeneralInfo

+ (NSString *)appName {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
}

+ (NSString *)appBuild {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary pl_stringForKey:@"CFBundleVersion" defaultValue:@"0"];
    return appVersion;
}

+ (NSString *)appVersion {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary pl_stringForKey:@"CFBundleShortVersionString" defaultValue:@"0"];
    return appVersion;
}

+ (NSString *)phoneVersion {
    return [[UIDevice currentDevice] systemVersion];
}

+ (NSString *)phoneModel {
    return [[UIDevice currentDevice] model];
}

+ (NSString *)localizedModel {
    return [[UIDevice currentDevice] localizedModel];
}

+ (UIImage *)appIcon {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoDictionary valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    return [UIImage imageNamed:icon];
}

+ (NSString *)bundleName {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary pl_stringForKey:@"CFBundleName" defaultValue:@"0"];
    return appVersion;
}

+ (NSString *)uniqueDeviceIdentifier
{
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
    
    NSString *strApplicationUUID = [SAMKeychain passwordForService:appName account:@"bonadePL"];
    if (strApplicationUUID == nil)
    {
        strApplicationUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        
        NSError *error = nil;
        SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
        query.service = appName;
        query.account = @"bonadePL";
        query.password = strApplicationUUID;
        query.synchronizationMode = SAMKeychainQuerySynchronizationModeNo;
        [query save:&error];
        
    }
    
    return strApplicationUUID;
}

@end
